package com.banyuan.HomeWork.小练习;

public class Alipay {
    private Account[] accounts = new Account[5];

    {
        accounts[0] = new Account("10001","111",5000);
        accounts[1] = new Account("10002","111",1000);
        accounts[2] = new Account("10003","111",2000);
        accounts[3] = new Account("10004","111",3000);
        accounts[4] = new Account("10005","111",4000);
    }

    public String serveForTransfer(String id,String password,String toId,int money){
        //1.校验卡号
        boolean flag1 = false;
        Account temp1 = null,temp2 = null;

        for (Account account : accounts) {
//            System.out.println("1"+ i);
            flag1 = account.getId().equals(id);
//            System.out.println(flag1);
            if (flag1) {
                temp1 = account;
                break;
            }
        }
//        System.out.println(flag1);
        if(!flag1){
            return "账户不存在!";
        }

        //2.校验密码
        if(temp1.getPassword() != password){
            return "密码错误！";
        }


        //3.对方卡号存在
        boolean flag2 = false;
        for (Account account : accounts) {
            flag2 = account.getId().equals(toId);
            if (flag2) {
                temp2 = account;
                break;
            }
        }

        if(!flag2){
            return "转账账户不存在!";
        }


        //4.不能自己转自己
        if (id == toId){
            return "不要自己给自己转账！";
        }

        //5.余额要足
        if(temp1.getMoney() < money){
            return "余额不足";
        }

        temp1.setMoney(temp1.getMoney() - money);
        temp2.setMoney(temp2.getMoney() + money);


        //6.转账成功
        return ("转账成功" + "当前账户还剩" + temp1.getMoney());
    }
}
